 /* 
 通过mutations 间接更新state的多个方法对象
 包含多个对应事件 回掉函数的对象
 */
 import { GET_ADDRESS, GET_GOODS, GET_SHOPS, GET_USER_INFO, CLEAR_USER_INFO, RECIVE_GOOD, INCREMENT_FOOD_COUNT, DECREMENT_FOOD_COUNT, CLEAR_COUNT_FOODS } from './mutation-types.js'
 import { reqAddres, reqFoodCategorys, reqShops, reqUserInfo, reqShopGood } from '../api/index.js'
 export default {
     //获取 初始地址
     async getAddress({ commit, state }) {
         const geohash = state.latitude + ',' + state.longitude
         const res = await reqAddres(geohash)
         const address = res
         commit(GET_ADDRESS, { address })
     },
     // 获取轮播图的 分类项
     async getGoods({ commit }) {
         const res = await reqFoodCategorys()
         const foodsList = res
         commit(GET_GOODS, { foodsList })
     },
     //获取首页的店铺列表
     async getShops({ commit, state }) {
         const res = await reqShops(state.longitude, state.latitude)
         const shops = res
         commit(GET_SHOPS, { shops })
     },
     //同步记录用户信息 
     storgeVuexInfo({ commit }, userInfo) {
         commit(GET_USER_INFO, { userInfo })
     },

     //异步获取用户信息,自动登录
     async reqUserInfo({ commit }) {
         const userInfo = await reqUserInfo(); //获取本地pc的cookie 发送到服务器 获取到用户信息
         commit(GET_USER_INFO, { userInfo })
     },

     //退出登录
     clearUserInfo({ commit }) {
         commit(CLEAR_USER_INFO)
     },

     //异步获取，接受店铺的分类 商品
     async reciveGood({ commit }, restaurant_id) {
         const shopGood = await reqShopGood({ restaurant_id })
         commit(RECIVE_GOOD, { shopGood })
     },
     //同步更新 shopGood[0].foods[0].count里的
     updataFoodCount({ commit }, { isAdd, food }) {
         if (isAdd) {
             commit(INCREMENT_FOOD_COUNT, { food })
         } else {
             commit(DECREMENT_FOOD_COUNT, { food })
         }
     },
     //同步清除 countFoods 内的数据
     clearCountFoods({ commit }) {
         commit(CLEAR_COUNT_FOODS)
     }

 }